package com.jch.dm8_thread_specific_storage.example;

import java.text.DecimalFormat;
import java.util.concurrent.*;

// 强随机数生成器客户端
public class SmsVerificationCodeSender {

    private static final ExecutorService EXECUTOR = new ThreadPoolExecutor(1, Runtime.getRuntime().availableProcessors(), 60, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r,"VerificationCodeSender");
            t.setDaemon(true);
            return t;
        }
    },new ThreadPoolExecutor.DiscardPolicy());

    public void sendVerificationSms(final String msisdn){
        Runnable task = new Runnable() {
            @Override
            public void run() {
                int verificationCode = ThreadSpecificSecureRandom.INSTANCE.nextInt(999999);
                DecimalFormat df = new DecimalFormat("000000");
                String txtVerCode = df.format(verificationCode);
                sendSms(msisdn,txtVerCode);
            }
        };
        EXECUTOR.submit(task);
    }

    private void sendSms(String msisdn,String verificationCode){
        System.out.println("Sending verification code " + verificationCode + " to " + msisdn);

    }

    public static void main(String[] args){
        SmsVerificationCodeSender client = new SmsVerificationCodeSender();
        client.sendVerificationSms("17890001111");
        client.sendVerificationSms("15689890000");
        client.sendVerificationSms("13411110000");
        try{
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
